package com.fqgj.xjd.user.service;


import com.fqgj.xjd.user.common.enums.PassEnum;
import com.fqgj.xjd.user.dao.UserAuthFaceRecognitionDao;
import com.fqgj.xjd.user.dao.UserAuthFaceRecognitionDetailDao;
import com.fqgj.xjd.user.dao.UserAuthFaceRecognitionHistoryDao;
import com.fqgj.xjd.user.client.response.UserAuthFaceRecognition;
import com.fqgj.xjd.user.client.response.UserAuthFaceRecognitionHistory;
import com.fqgj.xjd.user.entity.UserAuthFaceRecognitionDetailEntity;
import com.fqgj.xjd.user.entity.UserAuthFaceRecognitionEntity;
import com.fqgj.xjd.user.entity.UserAuthFaceRecognitionHistoryEntity;
import com.fqgj.xjd.user.client.enums.FaceRecognitionTypeEnum;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/3/7
 * Time: 上午10:07
 */
@Service
public class FaceRecognitionService {

    @Autowired
    private UserAuthFaceRecognitionDao userAuthFaceRecognitionDao;

    @Autowired
    private UserAuthFaceRecognitionDetailDao userAuthFaceRecognitionDetailDao;

    @Autowired
    private UserAuthFaceRecognitionHistoryDao userAuthFaceRecognitionHistoryDao;


    public Boolean add(UserAuthFaceRecognition userAuthFaceRecognition, FaceRecognitionTypeEnum faceRecognitionTypeEnum) {
        UserAuthFaceRecognitionDetailEntity userAuthFaceRecognitionDetailEntity = new UserAuthFaceRecognitionDetailEntity();
        BeanUtils.copyProperties(userAuthFaceRecognition, userAuthFaceRecognitionDetailEntity);
        int detailId = addDetail(userAuthFaceRecognitionDetailEntity);
        UserAuthFaceRecognitionEntity userAuthFaceRecognitionEntity = userAuthFaceRecognitionDao.selectUserAuthFaceRecognitionByUserCodeAndType(userAuthFaceRecognition.getUserCode(), faceRecognitionTypeEnum.getType());
        if (userAuthFaceRecognitionEntity != null) {
            userAuthFaceRecognitionDao.deleteByPrimaryKey(userAuthFaceRecognitionEntity.getId());
        }

        userAuthFaceRecognitionEntity = new UserAuthFaceRecognitionEntity();
        BeanUtils.copyProperties(userAuthFaceRecognition, userAuthFaceRecognitionEntity);
        userAuthFaceRecognitionEntity.setDetailId(detailId);
        userAuthFaceRecognitionEntity.setType(faceRecognitionTypeEnum.getType());
        return null != userAuthFaceRecognitionDao.insert(userAuthFaceRecognitionEntity);
    }

    public void addFiledHistory(UserAuthFaceRecognitionHistory userAuthFaceRecognitionHistory) {
        UserAuthFaceRecognitionDetailEntity userAuthFaceRecognitionDetailEntity = new UserAuthFaceRecognitionDetailEntity();
        BeanUtils.copyProperties(userAuthFaceRecognitionHistory, userAuthFaceRecognitionDetailEntity);
        int detailId = addDetail(userAuthFaceRecognitionDetailEntity);

        addHistory(userAuthFaceRecognitionHistory, detailId);
    }


    private int addDetail(UserAuthFaceRecognitionDetailEntity userAuthFaceRecognitionDetailEntity) {
        userAuthFaceRecognitionDetailEntity.setDeleted(false);
        userAuthFaceRecognitionDetailDao.insert(userAuthFaceRecognitionDetailEntity);
        return userAuthFaceRecognitionDetailEntity.getId().intValue();
    }


    private void addHistory(UserAuthFaceRecognitionHistory userAuthFaceRecognitionHistory, Integer detailId) {
        UserAuthFaceRecognitionHistoryEntity userAuthFaceRecognitionHistoryEntity = new UserAuthFaceRecognitionHistoryEntity();
        BeanUtils.copyProperties(userAuthFaceRecognitionHistory, userAuthFaceRecognitionHistoryEntity);
        userAuthFaceRecognitionHistoryEntity.setDetailId(detailId);
        userAuthFaceRecognitionHistoryDao.insert(userAuthFaceRecognitionHistoryEntity);
    }


    public List<UserAuthFaceRecognition> getUserAuthFaceRecognitionByUserCode(String userCode) {

        List<UserAuthFaceRecognitionEntity> userAuthFaceRecognitionEntityList = userAuthFaceRecognitionDao.selectUserAuthFaceRecognitionByUserCode(userCode);
        List<UserAuthFaceRecognition> userAuthFaceRecognitionList = new ArrayList<UserAuthFaceRecognition>();

        if (CollectionUtils.isNotEmpty(userAuthFaceRecognitionEntityList)) {
            for (UserAuthFaceRecognitionEntity userAuthFaceRecognitionEntity : userAuthFaceRecognitionEntityList) {
                UserAuthFaceRecognition userAuthFaceRecognition = new UserAuthFaceRecognition();
                if (userAuthFaceRecognitionEntity.getType() == FaceRecognitionTypeEnum.OTHER_RECONGNITION.getType()
                        || (userAuthFaceRecognitionEntity.getType() == FaceRecognitionTypeEnum.ALI_RECONGNITION.getType() && userAuthFaceRecognitionEntity.getPassed() == PassEnum.TURE.getCode())) {
                    BeanUtils.copyProperties(userAuthFaceRecognitionEntity, userAuthFaceRecognition);
                    if (userAuthFaceRecognitionEntity.getDetailId() != null) {
                        UserAuthFaceRecognitionDetailEntity userAuthFaceRecognitionDetailEntity = userAuthFaceRecognitionDetailDao.selectByPrimaryKey(userAuthFaceRecognitionEntity.getDetailId().longValue());
                        if (userAuthFaceRecognitionDetailEntity != null) {
                            userAuthFaceRecognition.setFrontDetail(userAuthFaceRecognitionDetailEntity.getFrontDetail())
                                    .setBackDetail(userAuthFaceRecognitionDetailEntity.getBackDetail())
                                    .setFaceDetail(userAuthFaceRecognitionDetailEntity.getFaceDetail());
                        }
                    }
                    userAuthFaceRecognitionList.add(userAuthFaceRecognition);
                }
            }
        }
        return userAuthFaceRecognitionList;
    }


    public List<UserAuthFaceRecognitionHistory> getUserAuthFaceRecognitionHistoryByUserCodeAndType(String userCode, List<Integer> types) {
        List<UserAuthFaceRecognitionHistoryEntity> userAuthFaceRecognitionHistoryEntityList = userAuthFaceRecognitionHistoryDao.selectUserAuthFaceRecognitionHistoryByUserCodeAndType(userCode, types);

        List<UserAuthFaceRecognitionHistory> userAuthFaceRecognitionHistoryList = new ArrayList<UserAuthFaceRecognitionHistory>();
        for (UserAuthFaceRecognitionHistoryEntity userAuthFaceRecognitionHistoryEntity : userAuthFaceRecognitionHistoryEntityList) {
            UserAuthFaceRecognitionHistory userAuthFaceRecognitionHistory = new UserAuthFaceRecognitionHistory();
            BeanUtils.copyProperties(userAuthFaceRecognitionHistoryEntity, userAuthFaceRecognitionHistory);

            userAuthFaceRecognitionHistoryList.add(userAuthFaceRecognitionHistory);
        }
        return userAuthFaceRecognitionHistoryList;
    }


    public void deleteFaceRecognitionHistoriesByIdsAndTypes(List<Long> ids, List<Integer> types) {
        userAuthFaceRecognitionHistoryDao.deleteByIdsAndTypes(ids, types);
    }

}
